package com.spring.sunflower.userinfo;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.spring.sunflower.R;
import com.spring.sunflower.base.BaseActivity;
import com.spring.sunflower.bean.AnchorDetailBean;
import com.spring.sunflower.bean.LabelBean;
import com.spring.sunflower.common.CommonContract;
import com.spring.sunflower.constant.Constant;

import org.jetbrains.annotations.NotNull;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Kevin on 2021/6/28<br/>
 * <p>
 * Describe:<br/>
 */
public class HobbyLabelActivity extends BaseActivity<HobbyLabelPresenter> implements CommonContract.HobbyLabelView, OnItemClickListener {
    private RecyclerView mRecyclerView;
    private TextView tvDone;
    private HobbyAdapter mAdapter;
    private List<LabelBean.DatasBean> mData = new ArrayList<>();
    private List<String> hobbyData = new ArrayList<>();
    private List<LabelBean.DatasBean> selectedHobbyData = new ArrayList<>();
    private String genderType;
    private List<AnchorDetailBean.DatasBean.HobbiesListBean> mList;
    private GradientDrawable btnBackground;

    @Override
    public void initView() {
        Intent intent = getIntent();
        mList = (List<AnchorDetailBean.DatasBean.HobbiesListBean>) intent.getSerializableExtra(Constant.USER_HOBBY_LABEL);
        tvTitle.setText("个性爱好");
        mRecyclerView = findViewById(R.id.rvRecyclerView);
        tvDone = findViewById(R.id.tvDone);
        GridLayoutManager layoutManager = new GridLayoutManager(this, 4);
        mRecyclerView.setLayoutManager(layoutManager);
        mAdapter = new HobbyAdapter(R.layout.adapter_item_hobby, mData);
        mRecyclerView.setAdapter(mAdapter);
        mPresenter.getHobbyType();
        mAdapter.setOnItemClickListener(this);
        btnBackground = (GradientDrawable) tvDone.getBackground();
        if (mList.size() > 1) {
            btnBackground.setColor(Color.parseColor("#FADB4A"));
        } else {
            btnBackground.setColor(Color.parseColor("#CCCCCC"));
        }
        tvDone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (hobbyData.size() > 1) {
                    StringBuffer buffer = new StringBuffer();
                    for (int i = 0; i < selectedHobbyData.size(); i++) {
                        if (i < selectedHobbyData.size() - 1) {
                            buffer.append(selectedHobbyData.get(i).getId());
                            buffer.append(",");
                        } else {
                            buffer.append(selectedHobbyData.get(i).getId());
                        }
                    }
                    Map<String, String> map = new HashMap<>();
                    map.put("hobbies", buffer.toString());
                    mPresenter.updateUserInfo(map);
//                    String string = handleHobbyData();
                }
            }
        });
    }

    @NotNull
    private String handleHobbyData() {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < hobbyData.size(); i++) {
            if (i < hobbyData.size() - 1) {
                stringBuffer.append(hobbyData.get(i) + ",");
            } else {
                stringBuffer.append(hobbyData.get(i));
            }
        }
        return stringBuffer.toString();
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_hobby_label;
    }

    @Override
    public HobbyLabelPresenter setPresenter() {
        return new HobbyLabelPresenter(this);
    }

    @Override
    public void showHobby(List<LabelBean.DatasBean> data) {
        if (mList.size() > 0) {
            for (AnchorDetailBean.DatasBean.HobbiesListBean hobbiesListBeans : mList) {
                for (LabelBean.DatasBean bean : data) {
                    if (bean.getId().equals(hobbiesListBeans.getId())) {
                        bean.setChecked(true);
                        selectedHobbyData.add(bean);
                    }
                }
                hobbyData.add(hobbiesListBeans.getId());
            }
        }
        mAdapter.addData(data);
    }

    @Override
    public void updateSuccess() {
        Intent intentX = new Intent(HobbyLabelActivity.this, UserInfoEditActivity.class);
        intentX.putExtra(Constant.USER_HOBBY_LABEL, (Serializable) selectedHobbyData);
        setResult(RESULT_OK, intentX);
        finish();
    }

    @Override
    public void updateFailed() {

    }

    @Override
    public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {

        TextView textView = (TextView) adapter.getViewByPosition(position, R.id.tvLabel);
        boolean enabled = textView.isEnabled();
        textView.setEnabled(!enabled);
        LabelBean.DatasBean datasBean = mData.get(position);
        GradientDrawable background = (GradientDrawable) textView.getBackground();
        if (!datasBean.isChecked()) {
            if (hobbyData.size() >= 5) {
                showToast("最多选5个");
                return;
            }
//            if (hobbyData.size() >= 5) {
//                int index = mData.indexOf(selectedHobbyData.get(0));
//                TextView tv = (TextView) adapter.getViewByPosition(index, R.id.tvLabel);
//                tv.setEnabled(!tv.isEnabled());
//                GradientDrawable b = (GradientDrawable) tv.getBackground();
//                b.setColor(Color.parseColor("#CCCCCC"));
//                hobbyData.remove(0);
//                selectedHobbyData.remove(0);
//            }
            datasBean.setChecked(true);
            background.setColor(Color.parseColor("#" + datasBean.getColor()));
            hobbyData.add(datasBean.getId());
            selectedHobbyData.add(datasBean);
        } else {
            background.setColor(Color.parseColor("#CCCCCC"));
            int index = hobbyData.indexOf(datasBean.getId());
            if (index != -1) {
                hobbyData.remove(index);
                selectedHobbyData.remove(index);
            }
            datasBean.setChecked(false);
        }
        mAdapter.notifyDataSetChanged();
        if (hobbyData.size() > 1) {
            btnBackground.setColor(Color.parseColor("#FADB4A"));
        } else {
            btnBackground.setColor(Color.parseColor("#CCCCCC"));
        }
    }
}
